import axios from 'axios'
import { Toast, Notify } from 'vant'
import qs from 'qs'
let baseURL = process.env.VUE_APP_BASE_API ? "http://123.207.32.32:8000/api/h8" : "http://123.207.32.32:8000/api/h8"
const service = axios.create({
    baseURL,
    timeout: 5000
})
//请求拦截
service.interceptors.request.use(
    config => {
        Toast.loading({
            duration: 0, // 持续展示 toast
            forbidClick: true,
            message: '加载中'
        })
        return config
    },
    error => {
        console.log(error)
        return Promise.reject(error)
    }
)
//响应拦截
service.interceptors.response.use(
    response => {
        Toast.clear();
        if (response.status === 200) {
            return response.data
        } else {
            Notify({
                type: 'danger',
                message: res.msg,
                duration: 1000 * 2
            });
            // return Promise.reject(new Error(res.msg || 'Error'))
        }
    },
    error => {
        Notify({
            type: 'danger',
            message: '网络有误',
            duration: 1000 * 5
        });
        Toast.clear();
        return Promise.reject(error)
    }
)
function ajax(type, url, data = {}) {
    if (Object.prototype.toString.call(data) === '[object Object]') {
        data = qs.stringify(data)
    }
    return service({
        url,
        method: type,
        data
    })
}
export function post(url, data = {}) {
    return ajax('post', url, data)
};
export function put(url, data = {}) {
    return ajax('put', url, data)
};
export function del(url, data = {}) {
    return ajax('delete', url, data)
};
export function get(url, data = {}) {
    return service({
        url,
        method: 'get',
        params: data
    })
}



